package com.shz.controller;


import com.shz.dto.MainAddress;
import com.shz.dto.RespBean;
import com.shz.pojo.City;
import com.shz.pojo.Provincial;
import com.shz.pojo.TbCourse;
import com.shz.pojo.TbPlace;
import com.shz.service.CityService;
import com.shz.service.ProvincialService;
import com.shz.service.TbCourseService;
import com.shz.service.TbPlaceService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shz
 * @since 2024-07-29
 */

@Api(tags = "基地模块")
@RestController
@RequestMapping("/tb-place")
public class TbPlaceController {
    @Resource
    TbPlaceService tbPlaceService;
    @Resource
    TbCourseService tbCourseService;
    @ApiOperation(value = "主页随机基地")
    @GetMapping("/getRandomCourse")
    public List<TbPlace> getRandomPlace(){
        List<TbPlace> list = tbPlaceService.query().list();
        if(list.size()>6){
            return list.subList(0, 6);
        }
        return list;
    }

    @ApiOperation(value = "基地详情")
    @GetMapping("/getCourse/{id}")
    public TbPlace getPlace(@PathVariable int id){
        return tbPlaceService.query().eq("id",id).one();
    }


    @Resource
    ProvincialService provincialService;
    @Resource
    CityService cityService;

    @ApiOperation(value = "查询所有省份和城市")
    @GetMapping("/getAllAddress")
    public List<MainAddress> getAllAddress(){
        List<Provincial> list = provincialService.query().list();
        List<MainAddress> li=new ArrayList<>();
        list.forEach(item->{
            List<City> children = cityService.query().eq("pid", item.getId()).list();
            List<MainAddress> zi=new ArrayList<>();
            for (City child : children) {
                MainAddress m = new MainAddress();
                m.setValue(child.getId());
                m.setLabel(child.getCity());
                zi.add(m);
            }
            MainAddress m =new MainAddress();
            m.setChildren(zi);
            m.setValue(item.getId());
            m.setLabel(item.getProvincial());
            li.add(m);
        });
         return li;
    }
    @ApiOperation(value = "根据省id查询基地")
    @ApiImplicitParam(name = "proId",value = "省id")
    @GetMapping("/getPlaceListByProvince/{proId}")
    public List<TbPlace> getPlaceListByProvince(@PathVariable int proId){
        return tbPlaceService.query().eq("proId", proId).list();
    }
    @ApiOperation(value = "根据省id和市id查询基地")
    @ApiImplicitParams({@ApiImplicitParam(name = "proId", value = "省id", required = true),
            @ApiImplicitParam(name = "citId", value = "市id", required = true),
    })
    @GetMapping("/getPlaceListByProvinceAndCity/{proId}/{citId}")
    public List<TbPlace> getPlaceListByProvince(@PathVariable int proId,@PathVariable int citId){
        return tbPlaceService.query().eq("proId", proId).eq("citId",citId).list();
    }

    @ApiOperation(value = "根据基地分类查询基地")
    @ApiImplicitParam(name = "bcid",value = "分类id")
    @GetMapping("/getPlaceListByCategory/{bcid}")
    public List<TbPlace> getPlaceListByCategory(@PathVariable int bcid){
        return tbPlaceService.query().eq("bcid", bcid).list();
    }

    @ApiOperation(value = "根据基地名查询基地")
    @GetMapping("/getPlaceByName/{name}")
    public List<TbPlace> getPlaceByName(@PathVariable String name){
        return tbPlaceService.query().like("name", name).list();
    }

    @ApiOperation(value = "热门基地")
    @GetMapping("/getHotPlaces")
    public List<TbPlace> getHotPlaces(){
        return tbPlaceService.query().eq("hot", true).list();
    }

}

